import tcp_server as tcp
import udp_server as udp
import os
import json
import time

if __name__!='__main__':
    os._exit(0)

config_file = './config.json'
if not os.path.exists(config_file):
    print("Error: None Configure File!")
    os._exit(0)

config_param = None
with open(config_file, 'r') as jf:
    config_param = json.load(jf)

if config_param is None:
    print("Error: None Configure Param!")
    os._exit(0)

udp_server_param = config_param["udp_server"]
tcp_server_param = config_param["tcp_server"]

udp_server_addr = (udp_server_param["addr"], udp_server_param["port"])
udp_test_server_addr = (udp_server_param["addr"], udp_server_param["test_port"])
tcp_server_addr = (tcp_server_param["addr"], tcp_server_param["port"])
tcp_auto_test_server_addr = (tcp_server_param["addr"], tcp_server_param["test_port"])

print("<<< TCP/UDP数据分发服务器 >>>\n")
print("---   Write by Awesome")
print("---   ouyanglei92@163.com\n\n")
print("参数配置: ")
param_message = "UDP Server (%s, %d)\nUDP Test Server (%s, %d)\n"\
                "TCP Server (%s, %d)\nTCP Test Server (%s, %d)\n" %\
                (udp_server_addr[0],udp_server_addr[1],\
                 udp_test_server_addr[0], udp_test_server_addr[1],\
                 tcp_server_addr[0], tcp_server_addr[1],\
                 tcp_auto_test_server_addr[0], tcp_auto_test_server_addr[1])
print(param_message)

udp.udp_server(udp_server_addr[0], udp_server_addr[1],  auto_test_en=False, client_timeout=udp_server_param["time_out"]).start() #分发服务器
udp.udp_server(udp_test_server_addr[0], udp_test_server_addr[1], auto_test_en=True, client_timeout=tcp_server_param["time_out"]).start() #自动回测服务器

tcp.tcp_server(tcp_server_addr[0], tcp_server_addr[1], maxcon=50, auto_test_en=False).start() #分发服务器
tcp.tcp_server(tcp_auto_test_server_addr[0], tcp_auto_test_server_addr[1], maxcon=50, auto_test_en=True).start() #自动回测服务器

while True:
    try:
        time.sleep(0.1)
    except:
        os._exit(0)


